﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using myMapObj;

namespace FakeGUI
{
    public partial class Form_LayerAttr : Form
    {
        private moMapLayer _Layer;
        public Form_LayerAttr(moMapLayer layer)
        {
            _Layer = layer;
            InitializeComponent();
        }

        private void LayerPr_Load(object sender, EventArgs e)
        {
            //初始化四个界面
            InitializeGeneral();
            InitializeSource();
            InitializeSymbol();
            InitializeLabel();
        }

        #region 常规界面

        private void InitializeGeneral()//初始化常规界面
        {
            TextBox_LayerName.Text = _Layer.Name;
            TextBox_Desciption.Text = _Layer.Description;

        }

        #endregion

        #region 源界面


        private void InitializeSource()
        {
            InitializeExtent();
            InitializeDataSource();
        }

        private void InitializeExtent()
        {
            moRectangle Envelope = _Layer.Extent;
            TextBox_Left.Text = Envelope.MinX.ToString("0.000000");
            TextBox_Right.Text = Envelope.MaxX.ToString("0.000000");
            TextBox_Up.Text = Envelope.MaxY.ToString("0.000000");
            TextBox_Down.Text = Envelope.MinY.ToString("0.000000");
        }

        private void InitializeDataSource()
        {
            string DataType = "数据类型：SEGV要素类";
            string GeometryType = "几何类型： " + _Layer.ShapeType.ToString();
            string GeoCoordinateSystem = "地理坐标系：WGS84";
            TextBox_DataSource.Text = DataType + "\n" + GeometryType + "\n" + GeoCoordinateSystem;
        }
        #endregion

        #region 符号系统界面
        private void InitializeSymbol()
        {
            moRenderer sRenderer = _Layer.Renderer;
            moSymbol sAbstractSymbol = null;
            //将所有字段加入到combobox里面
            for (int i = 0; i < _Layer.AttributeFields.Count; ++i)
            {
                moField sField = _Layer.AttributeFields.GetItem(i);
                ComboBox_Field.Items.Add(sField.Name);
                ComboBox_FieldForClassBreak.Items.Add(sField.Name);
            }
            if (_Layer.ShapeType == moGeometryTypeConstant.Point)
            {
                GroupBox_PointSymbol.Visible = true;
                GroupBox_LineSymbol.Visible = false;
            }
            else if (_Layer.ShapeType == moGeometryTypeConstant.MultiPolyline)
            {
                GroupBox_PointSymbol.Visible = false;
                GroupBox_LineSymbol.Visible = true;
            }
            else
            {
                GroupBox_PointSymbol.Visible = false;
                GroupBox_LineSymbol.Visible = false;
            }
            if (sRenderer.RendererType==moRendererTypeConstant.Simple)
            {
                RadioButton_SimpleRender.Checked = true;
                sAbstractSymbol = ((moSimpleRenderer)sRenderer).Symbol;
                if(sAbstractSymbol.SymbolType==moSymbolTypeConstant.SimpleFillSymbol)
                {
                    moSimpleFillSymbol sSymbol = (moSimpleFillSymbol)sAbstractSymbol;
                    PictureBox_Color.BackColor = sSymbol.Color;
                }
                else if(sAbstractSymbol.SymbolType==moSymbolTypeConstant.SimpleLineSymbol)
                {
                    moSimpleLineSymbol sSymbol = (moSimpleLineSymbol)sAbstractSymbol;
                    PictureBox_Color.BackColor = sSymbol.Color;
                }
                else
                {
                    moSimpleMarkerSymbol sSymbol = (moSimpleMarkerSymbol)sAbstractSymbol;
                    PictureBox_Color.BackColor = sSymbol.Color;
                }

            }
            else if (sRenderer.RendererType==moRendererTypeConstant.UniqueValue)
            {
                RadioButton_UniqueValue.Checked = true;
                moUniqueValueRenderer sUniqueValueRenderer = (moUniqueValueRenderer) (_Layer.Renderer);
                ComboBox_Field.SelectedItem = sUniqueValueRenderer.Field;
            }
            else
            {
                RadioButton_ClassBreaks.Checked = true;
                moClassBreaksRenderer sClassBreakRenderer = (moClassBreaksRenderer)(_Layer.Renderer);
                ComboBox_FieldForClassBreak.SelectedItem = sClassBreakRenderer.Field;
                for(int i = 0;i<sClassBreakRenderer.BreakCount;++i)
                {
                    ComboBox_BreakValue.Items.Add(sClassBreakRenderer.GetBreakValue(i).ToString());
                }
            }
        }

        private void RadioButton_SimpleRender_CheckedChanged(object sender, EventArgs e)
        {
            ShowProperBox();
        }

        private void RadioButton_UniqueValue_CheckedChanged(object sender, EventArgs e)
        {
            ShowProperBox();
        }

        private void RadioButton_ClassBreaks_CheckedChanged(object sender, EventArgs e)
        {
            ShowProperBox();
        }

        private void ShowProperBox()
        {
            if(RadioButton_SimpleRender.Checked)
            {
                GroupBox_SimpleRenderer.Visible = true;
                GroupBox_UniqueValue.Visible = false;
                GroupBox_ClassBreaks.Visible = false;
            }
            else if(RadioButton_UniqueValue.Checked)
            {
                GroupBox_SimpleRenderer.Visible = false;
                GroupBox_UniqueValue.Visible = true;
                GroupBox_ClassBreaks.Visible = false;
            }
            else
            {
                GroupBox_SimpleRenderer.Visible = false;
                GroupBox_UniqueValue.Visible = false;
                GroupBox_ClassBreaks.Visible = true;
            }
        }

        private void PictureBox_Color_Click(object sender, EventArgs e)
        {
            if (ColorDialog_SimpleRendererColor.ShowDialog() == DialogResult.OK)
            {
                PictureBox_Color.BackColor = ColorDialog_SimpleRendererColor.Color;
            }
        }

        private void Button_SelectColor_Click(object sender, EventArgs e)
        {
            //触发pictureBox的事件就行
            PictureBox_Color_Click(Button_SelectColor, e);
        }

        private void PictureBox_StartColor_Click(object sender, EventArgs e)
        {
            if (ColorDialog_StartColor.ShowDialog() == DialogResult.OK)
            {
                PictureBox_StartColor.BackColor = ColorDialog_StartColor.Color;
            }
        }

        private void PictureBox_EndColor_Click(object sender, EventArgs e)
        {
            if (ColorDialog_EndColor.ShowDialog() == DialogResult.OK)
            {
                PictureBox_EndColor.BackColor = ColorDialog_EndColor.Color;
            }
        }

        private void Button_Delete_Click(object sender, EventArgs e)
        {
            ComboBox_BreakValue.Items.Remove(ComboBox_BreakValue.SelectedItem);
        }

        private void Button_Add_Click(object sender, EventArgs e)
        {
            string sAdd = TextBox_AddValue.Text;
            ComboBox_BreakValue.Items.Add(sAdd);
        }

        private void TextBox_AddValue_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar) || (!TextBox_AddValue.Text.StartsWith("-") && e.KeyChar == '-' &&
                TextBox_AddValue.SelectionStart == 0) || (!TextBox_AddValue.Text.Contains('.') && e.KeyChar == '.') || e.KeyChar == '\b'))
                e.Handled = true;

        }
        #endregion

        #region 标注界面
        private void InitializeLabel()
        {
            for (int i = 0; i < _Layer.AttributeFields.Count; ++i)
            {
                moField sField = _Layer.AttributeFields.GetItem(i);
                ComboBox_FieldForLabel.Items.Add(sField.Name);
            }
            moLabelRenderer sLabelRenderer = _Layer.LabelRenderer;
            if(sLabelRenderer == null)
            {
                CheckBox_SetLabel.Checked = true;
            }
            else
            {
                TextBox_DisplayFont.Font = sLabelRenderer.TextSymbol.Font;
                TextBox_DisplayFont.ForeColor = sLabelRenderer.TextSymbol.FontColor;
            }

        }

        private void Button_ChangeFont_Click(object sender, EventArgs e)
        {
            if (FontDialog_LabelFont.ShowDialog() == DialogResult.OK)
            {
                TextBox_DisplayFont.Font = FontDialog_LabelFont.Font;
            }
        }

        private void PictureBox_FontColor_Click(object sender, EventArgs e)
        {
            if (ColorDialog_FontColor.ShowDialog() == DialogResult.OK)
            {
                TextBox_DisplayFont.ForeColor = ColorDialog_FontColor.Color;
                PictureBox_FontColor.BackColor = ColorDialog_FontColor.Color;
            }
        }
        #endregion

        #region 根据该界面的各参数，重新设定图层的属性
        public void SetLayerAttr(moMapLayer layer)
        {
            SetGeneral(layer);
            SetSymbol(layer);
            SetLabel(layer);
        }

        private void SetGeneral(moMapLayer layer)
        {
            layer.Name = TextBox_LayerName.Text;
            layer.Description = TextBox_Desciption.Text;
            layer.Visible = CheckBox_Visible.Checked;
        }

        private void SetSymbol(moMapLayer layer)
        {
            if(_Layer.Renderer.RendererType == moRendererTypeConstant.Simple)
            {
                Color sColor = PictureBox_Color.BackColor;
                if(_Layer.ShapeType == moGeometryTypeConstant.Point)
                {
                    moSimpleMarkerSymbol sSymbol = new moSimpleMarkerSymbol();
                    sSymbol.Color = sColor;
                    moSimpleMarkerSymbolStyleConstant sStyle = (moSimpleMarkerSymbolStyleConstant)
                        (Enum.Parse(typeof(moSimpleMarkerSymbolStyleConstant),(string)(ComboBox_PointSymbol.SelectedItem)));
                    sSymbol.Style = sStyle;
                    moSimpleRenderer sRenderer = new moSimpleRenderer();
                    sRenderer.Symbol = sSymbol;
                    _Layer.Renderer = sRenderer;
                }
                else if (_Layer.ShapeType == moGeometryTypeConstant.MultiPolyline)
                {
                    moSimpleLineSymbol sSymbol = new moSimpleLineSymbol();
                    sSymbol.Color = sColor;
                    moSimpleLineSymbolStyleConstant sStyle = (moSimpleLineSymbolStyleConstant)
                        (Enum.Parse(typeof(moSimpleLineSymbolStyleConstant), (string)(ComboBox_LineSymbol.SelectedItem)));
                    sSymbol.Style = sStyle;
                    moSimpleRenderer sRenderer = new moSimpleRenderer();
                    sRenderer.Symbol = sSymbol;
                    _Layer.Renderer = sRenderer;
                    
                }
                else
                {
                    moSimpleFillSymbol sSymbol = new moSimpleFillSymbol();
                    sSymbol.Color = sColor;
                    moSimpleRenderer sRenderer = new moSimpleRenderer();
                    sRenderer.Symbol = sSymbol;
                    _Layer.Renderer = sRenderer;
                }
            }
            else if (_Layer.Renderer.RendererType == moRendererTypeConstant.UniqueValue)
            {
                moUniqueValueRenderer sRenderer = new moUniqueValueRenderer();
                sRenderer.Field = ComboBox_Field.SelectedItem.ToString() ;
                List<string> sValues = new List<string>();
                Int32 sFeatureCount = _Layer.Features.Count;
                for (Int32 i = 0; i < sFeatureCount; ++i)
                {
                    string sValue = (string)_Layer.Features.GetItem(i).Attributes.GetItem(0);
                    sValues.Add(sValue);
                }
                sValues = sValues.Distinct().ToList();//去除重复
                Int32 sValueCount = sValues.Count;
                if(_Layer.ShapeType == moGeometryTypeConstant.Point)
                {
                    for (Int32 i = 0; i < sValueCount; ++i)
                    {
                        myMapObj.moSimpleMarkerSymbol sSymbol = new myMapObj.moSimpleMarkerSymbol();
                        sRenderer.AddUniqueValue(sValues[i], sSymbol);

                    }
                    sRenderer.DefaultSymbol = new myMapObj.moSimpleMarkerSymbol();
                }
                else if (_Layer.ShapeType == moGeometryTypeConstant.MultiPolyline)
                {
                    for (Int32 i = 0; i < sValueCount; ++i)
                    {
                        myMapObj.moSimpleLineSymbol sSymbol = new myMapObj.moSimpleLineSymbol();
                        sRenderer.AddUniqueValue(sValues[i], sSymbol);

                    }
                    sRenderer.DefaultSymbol = new myMapObj.moSimpleLineSymbol();
                }
                else
                {
                    for (Int32 i = 0; i < sValueCount; ++i)
                    {
                        myMapObj.moSimpleFillSymbol sSymbol = new myMapObj.moSimpleFillSymbol();
                        sRenderer.AddUniqueValue(sValues[i], sSymbol);

                    }
                    sRenderer.DefaultSymbol = new myMapObj.moSimpleFillSymbol();
                }
                _Layer.Renderer = sRenderer;
            }
            else//分级渲染
            {
                moClassBreaksRenderer sRenderer = new moClassBreaksRenderer();
                sRenderer.Field = ComboBox_FieldForClassBreak.Text;
                if(_Layer.ShapeType == moGeometryTypeConstant.Point)
                {
                    moSimpleMarkerSymbol sSymbol = new moSimpleMarkerSymbol();
                    for(int i = 0; i<ComboBox_BreakValue.Items.Count;++i)
                    {
                        double sValue = Convert.ToDouble(ComboBox_BreakValue.Items[i]);
                        sRenderer.AddBreakValue(sValue, sSymbol);
                    }
                }
                else if (_Layer.ShapeType == moGeometryTypeConstant.MultiPolyline)
                {
                    moSimpleLineSymbol sSymbol = new moSimpleLineSymbol();
                    for (int i = 0; i < ComboBox_BreakValue.Items.Count; ++i)
                    {
                        double sValue = Convert.ToDouble(ComboBox_BreakValue.Items[i]);
                        sRenderer.AddBreakValue(sValue, sSymbol);
                    }
                }
                else
                {
                    moSimpleFillSymbol sSymbol = new moSimpleFillSymbol();
                    for (int i = 0; i < ComboBox_BreakValue.Items.Count; ++i)
                    {
                        double sValue = Convert.ToDouble(ComboBox_BreakValue.Items[i]);
                        sRenderer.AddBreakValue(sValue, sSymbol);
                    }
                }
                sRenderer.RampColor(PictureBox_StartColor.BackColor, PictureBox_EndColor.BackColor);
                _Layer.Renderer = sRenderer;
            }
        }
        private void SetLabel(moMapLayer layer)
        {
            moLabelRenderer sLabelRenderer = new moLabelRenderer();
            sLabelRenderer.Field = (string)ComboBox_FieldForLabel.SelectedItem;
            sLabelRenderer.TextSymbol.Font = TextBox_DisplayFont.Font;
            sLabelRenderer.TextSymbol.FontColor = TextBox_DisplayFont.ForeColor;
            sLabelRenderer.LabelFeatures = CheckBox_SetLabel.Checked;
            _Layer.LabelRenderer = sLabelRenderer;

        }


        #endregion


    }
}
